# -*- coding: utf-8 -*-
"""
@Time    : 2024/7/12 15:53 
@Author  : ZhangShenao 
@File    : square_sum_multi_thread.py
@Desc    : 多线程版本——给定一个list,针对计算list中的每一个元素,计算从0开始到该元素的平方和
"""
import concurrent.futures

from bacis.decorator.perf_log_decorator import perf_log


def calc_square_sum(n: int) -> None:
    """
    计算从0开始到n的平方和
    """
    result = sum([i ** 2 for i in range(0, n + 1)])
    print(f'The square sum of {n} is {result}.')


def calc_all_square_sum(numbers: list[int]) -> None:
    """
    给定一个list,针对计算list中的每一个元素,计算从0开始到该元素的平方和
    :param numbers: 元素list
    """

    # 创建线程池
    with concurrent.futures.ThreadPoolExecutor() as executor:
        # 提交异步任务
        executor.map(calc_square_sum, numbers)


@perf_log
def run():
    numbers = [10000000 + x for x in range(20)]
    calc_all_square_sum(numbers)


run()

# [run] costs 11965.61 ms
